package com.yywz.io;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipTest {

	public static void main(String[] args) throws IOException {
		File file = new File("D:\\log");
		File[] fs = file.listFiles();

		for (int i = 0; i < fs.length; i++) {
			File zip = new File(fs[i].getPath() + ".zip");
			FileOutputStream fos = new FileOutputStream(zip);
			ZipOutputStream zos = new ZipOutputStream(fos);
			zip(fs[i], zos, fs[i].getName());
			zos.finish();
		}

	}

	private static void zip(File file, ZipOutputStream zos, String base) {
		if (file.isDirectory()) {
			try {
				zos.putNextEntry(new ZipEntry(base + "/"));

				base = base + "/";

				File[] fs = file.listFiles();

				for (int i = 0; i < fs.length; i++) {
					zip(fs[i], zos, base + fs[i].getName());
				}

			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			try {
				zos.putNextEntry(new ZipEntry(base));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
